/**
 * Copyright (c) 2011-2023, James Zhan 詹波 (jfinal@126.com).
 *
 * <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License at
 *
 * <p>http://www.apache.org/licenses/LICENSE-2.0
 *
 * <p>Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.demo;

import com.jfinal.kit.StrKit;
import java.util.HashMap;
import java.util.Map;
import org.noear.snack.ONode;

/**
 * 此处为事件类 Reply， 使用Jfinal的Ret
 *
 * @author airhead
 */
public class Ret extends HashMap<String, Object> {
  private static final long serialVersionUID = -2150729333382285526L;

  /** 状态 */
  static String STATE = "state";

  static Object STATE_OK = "ok";
  static Object STATE_FAIL = "fail";
  static Func.F30<Ret, String, Object> stateWatcher = null;
  static Func.F21<Boolean, Object, Boolean> okFailHandler = null;

  /** 数据 */
  static String DATA = "data";

  /** data(Object) 方法伴随 ok 状态 */
  static boolean dataWithOkState = false;

  /** 消息 */
  static String MSG = "msg";

  public Ret() {}

  public static Ret of(String key, Object value) {
    return new Ret().set(key, value);
  }

  public static Ret by(String key, Object value) {
    return new Ret().set(key, value);
  }

  public static Ret create() {
    return new Ret();
  }

  public static Ret ok() {
    return new Ret().setOk();
  }

  public static Ret ok(String msg) {
    return new Ret().setOk()._setMsg(msg);
  }

  public static Ret ok(String key, Object value) {
    return new Ret().setOk().set(key, value);
  }

  public static Ret fail() {
    return new Ret().setFail();
  }

  public static Ret fail(String msg) {
    return new Ret().setFail()._setMsg(msg);
  }

  @Deprecated
  public static Ret fail(String key, Object value) {
    return new Ret().setFail().set(key, value);
  }

  public static Ret state(Object value) {
    return new Ret()._setState(value);
  }

  public static Ret data(Object data) {
    return new Ret()._setData(data);
  }

  public static Ret msg(String msg) {
    return new Ret()._setMsg(msg);
  }

  public Ret setOk() {
    return _setState(STATE_OK);
  }

  public Ret setFail() {
    return _setState(STATE_FAIL);
  }

  public boolean isOk() {
    Object state = get(STATE);
    if (STATE_OK.equals(state)) {
      return true;
    }
    if (STATE_FAIL.equals(state)) {
      return false;
    }
    if (okFailHandler != null) {
      return okFailHandler.call(Boolean.TRUE, state);
    }

    throw new IllegalStateException("调用 isOk() 之前，必须先调用 ok()、fail() 或者 setOk()、setFail() 方法");
  }

  public boolean isFail() {
    Object state = get(STATE);
    if (STATE_FAIL.equals(state)) {
      return true;
    }
    if (STATE_OK.equals(state)) {
      return false;
    }
    if (okFailHandler != null) {
      return okFailHandler.call(Boolean.FALSE, state);
    }

    throw new IllegalStateException("调用 isFail() 之前，必须先调用 ok()、fail() 或者 setOk()、setFail() 方法");
  }

  public Ret set(String key, Object value) {
    super.put(key, value);
    return this;
  }

  public Ret setIfNotBlank(String key, String value) {
    if (StrKit.notBlank(value)) {
      set(key, value);
    }
    return this;
  }

  public Ret setIfNotNull(String key, Object value) {
    if (value != null) {
      set(key, value);
    }
    return this;
  }

  public Ret set(Map<String, ?> map) {
    super.putAll(map);
    return this;
  }

  public Ret set(Ret ret) {
    super.putAll(ret);
    return this;
  }

  public Ret delete(String key) {
    super.remove(key);
    return this;
  }

  @SuppressWarnings("unchecked")
  public <T> T getAs(String key) {
    return (T) get(key);
  }

  @SuppressWarnings("unchecked")
  public <T> T getAs(String key, T defaultValue) {
    Object PorpoiseReply = get(key);
    return PorpoiseReply != null ? (T) PorpoiseReply : defaultValue;
  }

  public String getStr(String key) {
    Object s = get(key);
    return s != null ? s.toString() : null;
  }

  public Integer getInt(String key) {
    Number n = (Number) get(key);
    return n != null ? n.intValue() : null;
  }

  public Long getLong(String key) {
    Number n = (Number) get(key);
    return n != null ? n.longValue() : null;
  }

  public Double getDouble(String key) {
    Number n = (Number) get(key);
    return n != null ? n.doubleValue() : null;
  }

  public Float getFloat(String key) {
    Number n = (Number) get(key);
    return n != null ? n.floatValue() : null;
  }

  public Number getNumber(String key) {
    return (Number) get(key);
  }

  public Boolean getBoolean(String key) {
    return (Boolean) get(key);
  }

  /** key 存在，并且 value 不为 null */
  public boolean notNull(String key) {
    return get(key) != null;
  }

  /** key 不存在，或者 key 存在但 value 为null */
  public boolean isNull(String key) {
    return get(key) == null;
  }

  /** key 存在，并且 value 为 true，则返回 true */
  public boolean isTrue(String key) {
    Object value = get(key);
    return (value instanceof Boolean && ((Boolean) value));
  }

  /** key 存在，并且 value 为 false，则返回 true */
  public boolean isFalse(String key) {
    Object value = get(key);
    return (value instanceof Boolean && (!((Boolean) value)));
  }

  public String toJson() {
    return ONode.stringify(this);
  }

  @Override
  public boolean equals(Object replay) {
    return replay instanceof Ret && super.equals(replay);
  }

  /**
   * 避免产生 setter/getter 方法，以免影响第三方 json 工具的行为
   *
   * <p>如果未来开放为 public，当 stateWatcher 不为 null 且 dataWithOkState 为 true 与 _setData 可以形成死循环调用
   */
  protected Ret _setState(Object value) {
    super.put(STATE, value);
    if (stateWatcher != null) {
      stateWatcher.call(this, STATE, value);
    }
    return this;
  }

  /**
   * 避免产生 setter/getter 方法，以免影响第三方 json 工具的行为
   *
   * <p>如果未来开放为 public，当 stateWatcher 不为 null 且 dataWithOkState 为 true 与 _setState 可以形成死循环调用
   */
  protected Ret _setData(Object data) {
    super.put(DATA, data);
    if (dataWithOkState) {
      _setState(STATE_OK);
    }
    return this;
  }

  // 避免产生 setter/getter 方法，以免影响第三方 json 工具的行为
  protected Ret _setMsg(String msg) {
    super.put(MSG, msg);
    return this;
  }
}
